<template>
  <!-- 销售价报表 -->
  <div class="salesMargin">
    <div class="header">
      <search
        :options="selectData"
        :search-son="searchSon"
        :msg="tacitly"
        @seek="collect"
        @reload="getData"
        @hand="handleQuery()"
      />
      <el-button type="primary" class="btn" size="mini" @click="handleQuery">
        搜索
      </el-button>
      <el-button
        class="btn1"
        type="primary"
        size="mini"
        @click="handleExport"
      >
        导出Excel
      </el-button>
      <filtration
        class="btn1"
        :ftype="ftype"
        :table-header="tableHeader1"
        :detail-header="tableHeader"
        :select-data="selectData"
        @seachData="handleQuery"
      />
    </div>
    <div class="table-content">
      <jc-table
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        show-summary
        :get-summaries="getSummaries"
      >
        <!--物料编码-->
        <template v-slot:btnStates="col">
          <el-link
            type="primary"
            @click="jumpMateriel1(col.scope.row.fmaterialNumber)"
          >
            {{ col.scope.row.fmaterialNumber }}
          </el-link>
        </template>
      </jc-table>
    </div>
    <!--    <div style="margin-top: 5px;">-->
    <!--      <el-tabs v-model="fnumber" type="card" @tab-click="getData1">-->
    <!--        <el-tab-pane v-for="item in numData" :key="item.fid" :label="item.fname" :name="item.fid+''" />-->
    <!--      </el-tabs>-->
    <!--    </div>-->
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        :total="total"
        :page.sync="currentPage"
        :limit.sync="size"
        @pagination="getData"
      />
    </div>
  </div>
</template>

<script>
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import { saleUnitPrice } from '@/api/financial/salesMargin'
import search from '@/components/Search'

export default {
  components: {
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      fnumber: '',
      conceal: 1,
      ftype: 97,
      val: '',
      searInput: {},
      total: 0, // 总条目
      currentPage: 1, // 当前页
      size: 200, // 每页显示多少条数据
      tableHeight: '73vh',
      // 表头
      tableData: [], // 销售数据
      tableHeader: [
        { label: this.$t('market.year2'), prop: 'fYear', width: '80' },
        { label: this.$t('market.custname'), prop: 'fcustomerName', width: '120' },
        { label: this.$t('market.fmodel'), prop: 'fmodel', width: '120' },
        { label: this.$t('market.fmaterial'), type: 'states', prop: 'fmaterialNumber', width: '120' },
        { label: this.$t('shop.fname2'), prop: 'fmaterialName', width: '120' },
        { label: this.$t('shop.fspecificaTion'), prop: 'fspecification', width: '120' },
        { label: this.$t('shop.unit'), prop: 'funitName', width: '80' },
        { label: this.$t('market.currency2'), prop: 'fcurrencyName', width: '80' },
        { label: this.$t('market.xxprice'), prop: 'ftaxprice', width: '80' },
        { label: this.$t('market.fdescripTion'), prop: 'fdescription', minWidth: '200' }
      ],
      tableHeader1: [],
      numData: [
        { fname: '管料', fid: '' },
        { fname: '面料', fid: '' },
        { fname: '海绵', fid: '' },
        { fname: '包材', fid: '' }
      ]
    }
  },
  created() {
    // this.getData()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 250px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map(res => {
          return res.label
        })
        const filterVal = this.tableHeader.map(res => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          saleUnitPrice({
            pageSize: 100000,
            pageNum: 1,
            search: this.searCollData.concat(this.details)
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '销售单价报表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    getData1(ev) {
      this.fnumber = ev
      this.getData()
    },
    // 获取销售毛利列表数据
    async getData(ev) {
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        search: this.searCollData.concat(this.details)
      }
      await saleUnitPrice(DATA).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.list
          this.total = res.data.total
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 搜索
    handleQuery(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.customHandle(ev)
        this.getData(ev.detail)
      } else {
        this.getData()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.salesMargin {
  @include listBom;
  padding: 0;
  .productImg {
    max-width: 50px;
    max-height: 30px;
  }
  .header {
    position: relative;
    margin-bottom: 10px;
    .btn {
      transform: translateY(32%);
      margin-left: 470px;
    }
    .btn1 {
      transform: translateY(32%);
    }
  }
}
</style>
